package com.getan.boot.module.system.controller;

import com.getan.boot.common.Pagination;
import com.getan.boot.common.ResponseBuilder;
import com.getan.boot.common.ResponseCodeData;
import com.getan.boot.common.annotation.OperateLog;
import com.getan.boot.common.domain.bo.CodeBO;
import com.getan.boot.common.domain.bo.IdsBO;
import com.getan.boot.common.enums.EnableStatus;
import com.getan.boot.jdbc.customquery.CustomQueryBO;
import com.getan.boot.module.system.domain.bo.DictBO;
import com.getan.boot.module.system.entity.DictEntity;
import com.getan.boot.module.system.entity.DictTypeEntity;
import com.getan.boot.module.system.service.persistent.DictService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典管理
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict")
public class DictController {

    private final String MODULE = "字典管理";
    private final DictService dictService;

    @PostMapping("search")
    public ResponseCodeData<Pagination<DictTypeEntity>> search(@RequestBody @Valid CustomQueryBO bo) {
        return ResponseBuilder.ok(dictService.searchType(bo));
    }

    @PostMapping("listItems")
    public ResponseCodeData<List<DictEntity>> listItems(@RequestBody @Valid CodeBO bo) {
        return ResponseBuilder.ok(dictService.listByType(bo.getCode(), false));
    }

    @OperateLog(module = MODULE, businessType = "ADD")
    @PostMapping("add")
    public ResponseCodeData<Long> add(@RequestBody @Valid DictBO.TypeAddBO bo) {
        DictTypeEntity entity = new DictTypeEntity();
        BeanUtils.copyProperties(bo, entity);
        dictService.checkTypeCode(entity);
        dictService.insertType(entity);
        return ResponseBuilder.ok(entity.getId());
    }

    @OperateLog(module = MODULE, businessType = "UPDATE")
    @PostMapping("update")
    public ResponseCodeData<Long> update(@RequestBody @Valid DictBO.TypeUpdateBO bo) {
        DictTypeEntity entity = new DictTypeEntity();
        BeanUtils.copyProperties(bo, entity);
        dictService.typeCanUpdate(bo.getId());
        dictService.updateType(entity);
        return ResponseBuilder.ok(entity.getId());
    }

    @OperateLog(module = MODULE, businessType = "DELETE")
    @PostMapping("delete")
    public ResponseCodeData<Void> delete(@RequestBody @Valid IdsBO bo) {
        List<DictTypeEntity> dbs = dictService.typeCanDelete(bo.getIds());
        dictService.deleteType(dbs);
        return ResponseBuilder.ok();
    }

    @OperateLog(module = MODULE, businessType = "DISABLE")
    @PostMapping("disable")
    public ResponseCodeData<Void> disable(@RequestBody @Valid IdsBO bo) {
        List<DictTypeEntity> dbs = dictService.typeCanDisable(bo.getIds());
        dictService.disableType(dbs);
        return ResponseBuilder.ok();
    }

    @OperateLog(module = MODULE, businessType = "ENABLE")
    @PostMapping("enable")
    public ResponseCodeData<Void> enable(@RequestBody @Valid IdsBO bo) {
        dictService.typeCanEnable(bo.getIds());
        dictService.enableType(bo.getIds());
        return ResponseBuilder.ok();
    }

    @OperateLog(module = MODULE, businessType = "ADD_ITEM")
    @PostMapping("addItem")
    public ResponseCodeData<Long> addItem(@RequestBody @Valid DictBO.AddBO bo) {
        DictEntity entity = new DictEntity();
        BeanUtils.copyProperties(bo, entity);
        DictTypeEntity type = dictService.typeCanUpdate(bo.getTypeId());
        entity.setType(type.getType());
        dictService.checkItemCode(entity);
        entity.setStatus(EnableStatus.ENABLE.getCode());
        entity.setSort(entity.getSort() == null ? 0 : entity.getSort());
        dictService.insert(entity);
        return ResponseBuilder.ok(entity.getId());
    }

    @OperateLog(module = MODULE, businessType = "UPDATE_ITEM")
    @PostMapping("updateItem")
    public ResponseCodeData<Long> updateItem(@RequestBody @Valid DictBO.UpdateBO bo) {
        DictEntity entity = new DictEntity();
        BeanUtils.copyProperties(bo, entity);
        dictService.canUpdate(bo.getId());
        dictService.update(entity);
        return ResponseBuilder.ok(entity.getId());
    }

    @OperateLog(module = MODULE, businessType = "DELETE_ITEM")
    @PostMapping("deleteItem")
    public ResponseCodeData<Void> deleteItem(@RequestBody @Valid IdsBO bo) {
        dictService.canDelete(bo.getIds());
        dictService.deleteBatch(bo.getIds());
        return ResponseBuilder.ok();
    }

    @OperateLog(module = MODULE, businessType = "DISABLE_ITEM")
    @PostMapping("disableItem")
    public ResponseCodeData<Void> disableItem(@RequestBody @Valid IdsBO bo) {
        dictService.canDisable(bo.getIds());
        dictService.disable(bo.getIds());
        return ResponseBuilder.ok();
    }

    @OperateLog(module = MODULE, businessType = "ENABLE_ITEM")
    @PostMapping("enableItem")
    public ResponseCodeData<Void> enableItem(@RequestBody @Valid IdsBO bo) {
        dictService.canEnable(bo.getIds());
        dictService.enable(bo.getIds());
        return ResponseBuilder.ok();
    }

}
